package com.tanhua.app.service;


import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;

import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.domain.db.UserInfo;

import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserHolder;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity location(double y_longitude, double x_latitude, String addrStr) {

        Long userId = UserHolder.getUser().getId();
        //当前用户 经度 纬度 对应位置
        userLocationApi.findUser(userId,y_longitude,x_latitude,addrStr);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity search(String gender, Integer distance) {

        List<NearUserVo> list=new ArrayList();

        //2 根据当前登录用户的地理信息以接受到的距离划分半径 做附近人地理信息的搜索
        List<UserLocationVo> userLocationList = userLocationApi.search(UserHolder.getUser().getId(), distance);
        if(CollectionUtil.isNotEmpty(userLocationList)){
            for (UserLocationVo userLocationVo : userLocationList) {
                //UserInfo
                UserInfo userInfo = userInfoApi.findUserInfo(userLocationVo.getUserId());
                //把自己排除
                if(UserHolder.getUser().getId()==userInfo.getId()){
                    continue;
                }
                //按选择的性别排除
                if(!gender.equals(userInfo.getGender())){
                    continue;
                }
                //封装vo
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(userInfo.getId());
                nearUserVo.setNickname(userInfo.getNickname());
                nearUserVo.setAvatar(userInfo.getAvatar());
                list.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(list);
    }
}
